package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.query.EmployeeQueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {

	@Autowired
	private EmployeeMapper employeeMapper;

	public void delete(Long id) {
		employeeMapper.delete(id);
		employeeMapper.deleteRelation(id);
	}

	public void batchDelete(Long[] ids) {
		employeeMapper.batchDelete(ids);
	}

	public void insert(Employee employee, Long[] ids) {
		employeeMapper.insert(employee);
		if(ids != null){
			for (Long roleId : ids) {
				employeeMapper.insertRelation(employee.getId(),roleId);
			}
		}
	}

	public Employee get(Long id) {
		return employeeMapper.get(id);
	}

	public List<Employee> selectAll() {
		return employeeMapper.selectAll();
	}

	// 分页
	public PageInfo<Employee> query(EmployeeQueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
		return new PageInfo<Employee>(employeeMapper.selectForList(qo));
	}

	// 根据名字(或名字_id)查询单个员工
	public Employee selectByUsernameAndId(String username, Long id){
		return employeeMapper.selectByUsernameAndId(username,id);
	}

	public Workbook exportXls() {
		List<Employee> employees = employeeMapper.selectAll();
		// 使用poi 创建excel
		Workbook wb = new HSSFWorkbook();
		// 创建excel 表
		Sheet sheet = wb.createSheet("员工信息表");
		// 创建行
		Row row = sheet.createRow(0);
		// 创建列,并且起标题
		row.createCell(0).setCellValue("姓名");
		row.createCell(1).setCellValue("邮箱");
		row.createCell(2).setCellValue("年龄");
		for (int i = 0; i < employees.size(); i++) {
			Employee employee = employees.get(i);
			// 设置每一行的内容
			row = sheet.createRow(i+1);
			row.createCell(0).setCellValue(employee.getName());
			row.createCell(1).setCellValue(employee.getEmail());
			row.createCell(2).setCellValue(employee.getAge());
		}
		return wb;
	}

	@Override
	public List<Employee> selectByRoleSn(String...sns) {
		return employeeMapper.selectByRoleSn(sns);
	}

	public void importXls(MultipartFile file) throws IOException {
		// 拿到上传过来的文件
		HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
		// 拿到excel 中的第一张表
		HSSFSheet sheet = wb.getSheetAt(0);
		// 拿到最大索引
		int lastRowNum = sheet.getLastRowNum();
		for (int i = 1; i <= lastRowNum; i++) {
			HSSFRow row = sheet.getRow(i);
			String name = row.getCell(0).getStringCellValue();
			// 名字为空则结束遍历
			if(!StringUtil.hasLength(name)){
				return;
			}
			Employee employee = new Employee();
			employee.setName(name);
			employee.setEmail(row.getCell(1).getStringCellValue());
			HSSFCell cell = row.getCell(2);
			if(cell.getCellType() == CellType.NUMERIC){
				employee.setAge(Integer.valueOf((int)cell.getNumericCellValue()));
			}else {
				employee.setAge(Integer.valueOf(cell.getStringCellValue()));
			}
			employee.setPassword("1");
			employeeMapper.insert(employee);
		}
	}

	// 查询员工拥有的权限表达式
	public List<String> selectByEmployeeIdForExpression(Long id) {
		return employeeMapper.selectByEmployeeIdForEXPRESSION(id);
	}

	// 修改密码
	public void updatePassword(Long id, String newPassword) {
		employeeMapper.updatePassword(id,newPassword);
	}

	// 禁用/恢复
	public void updateStatus(Long id) {
		if (id != null) {
			employeeMapper.updateStatus(id);
		}else {
			throw new RuntimeException("操作失败");
		}
	}

	//  员工修改
	public void update(Employee employee, Long[] ids) {
		employeeMapper.deleteRelation(employee.getId());
		if(ids != null){
			for (Long roleId : ids) {
				employeeMapper.insertRelation(employee.getId(),roleId);
			}
		}
		employeeMapper.update(employee);
	}
}
